﻿/* ================================================================
 * Lyon Utilities
 * Author: Lyon Tam 
 * Author's email: imdingji (at) gmail.com 
 * Author's Blog: dingji.cnblogs.com
 * Create Date: 2011-20-20
 * ==============================================================*/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace Lyon.Util.Tools
{
    public class TypeParser
    {
        #region 类型转换

        /// <summary>
        /// 将对象转成整型（Int32），如果不成功，返回默认值defaultValue
        /// </summary>
        /// <param name="expression">要转换的对象</param>
        /// <param name="defaultValue">默认值</param>
        /// <returns>转换后的整型值</returns>
        public static int String2Int(object expression, int defaultValue)
        {
            if (expression != null)
            {
                string str = expression.ToString();
                int tmp;
                if (int.TryParse(str, out tmp))
                    return tmp;
            }
            return defaultValue;
        }

        /// <summary>
        /// 将对象转成整型，如果不成功，返回0
        /// </summary>
        /// <param name="expression">要转换的对象</param>
        /// <returns>转换后的整型值</returns>
        public static int String2Int(object expression)
        {
            return String2Int(expression, 0);
        }

        /// <summary>
        /// 将对象转成单精度型（Float），如果不成功，返回默认值defaultValue
        /// </summary>
        /// <param name="expression">要转换的对象</param>
        /// <param name="defaultValue">默认值</param>
        /// <returns>转换后的单精度值</returns>
        public static float String2Float(object expression, float defaultValue)
        {
            if (expression != null)
            {
                string str = expression.ToString();
                float tmp;
                if (float.TryParse(str, out tmp))
                    return tmp;
            }
            return defaultValue;
        }

        /// <summary>
        /// 将对象转成单精度型（Float），如果不成功，返回0
        /// </summary>
        /// <param name="expression">要转换的对象</param>
        /// <returns>转换后的单精度值</returns>
        public static float String2Float(object expression)
        {
            return String2Float(expression, 0);
        }

        /// <summary>
        /// 将对象转成双精度型（Double），如果不成功，返回默认值
        /// </summary>
        /// <param name="expression">要转换的对象</param>
        /// <param name="defaultValue">默认值</param>
        /// <returns>转换后的双精度值</returns>
        public static double String2Double(object expression, double defaultValue)
        {
            if (expression != null)
            {
                string str = expression.ToString();
                double tmp;
                if (double.TryParse(str, out tmp))
                    return tmp;
            }
            return defaultValue;
        }

        /// <summary>
        /// 将对象转成双精度型（Double），如果不成功，返回0
        /// </summary>
        /// <param name="expression">要转换的对象</param>
        /// <returns>转换后的双精度值</returns>
        public static double String2Double(object expression)
        {
            return String2Double(expression, 0);
        }

        /// <summary>
        /// 将对象转成日期（DateTime），如果不成功，返回DefaultDateTime
        /// </summary>
        /// <param name="expression">要转换的对象</param>
        /// <param name="defaultDateTime">默认值</param>
        /// <returns>转换后的日期</returns>
        public static DateTime String2DateTime(object expression, DateTime defaultDateTime)
        {
            if (expression != null)
            {
                DateTime tmp;
                if (DateTime.TryParse(expression.ToString(), out tmp))
                    return tmp;
            }
            return defaultDateTime;
        }        

        /// <summary>
        /// 将对象转成日期（DateTime），如果不成功，返回Now
        /// </summary>
        /// <param name="expression">要转换的对象</param>
        /// <returns>转换后的日期</returns>
        public static DateTime String2DateTime(object expression)
        {
            return String2DateTime(expression, DateTime.Now);
        }

        /// <summary>
        /// 将对象转成日期（DateTime），如果不成功，返回0001/01/01
        /// </summary>
        /// <param name="expression">要转换的对象</param>
        /// <returns>转换后的日期</returns>
        public static DateTime String2DateTime1(object expression)
        {
            return String2DateTime(expression, DateTime.MinValue);
        }

        #endregion

        #region 类型判断

        /// <summary>
        /// 判断对象是否为合法整数
        /// </summary>
        /// <param name="s"></param>
        /// <returns></returns>
        public static bool IsInteger(object s)
        {
            int r;
            if (s == null) return false;
            if (!int.TryParse(s.ToString(), out r)) return false;
            return true;            
        }

        /// <summary>
        /// 判断对象串是否为合法数字(包含小数，正负)
        /// </summary>
        /// <param name="s"></param>
        /// <returns></returns>
        public static bool IsNumber(object s)
        {
            double r;
            if (s == null)  return false;            
            if (!double.TryParse(s.ToString(), out r))  return false;  
            return true; 
        }

        /// <summary>
        /// 判断对象是否为合法日期
        /// </summary>
        /// <param name="s"></param>
        /// <returns></returns>
        public static bool IsDateTime(object s)
        {
            DateTime r;
            if (s == null) return false;
            if (!DateTime.TryParse(s.ToString(), out r)) return false;
            return true; 
        }

        #endregion
    }
}
